// 键盘事件
// 注意：编辑层需要和事件处理打交道
export class KeyboardEvent {
    constructor (editor, container) {
        this.editor = editor;
        this.container = container;
    }
    beforeInputHandler_ (e) {
        const inputType = e.inputType;
        e.preventDefault(); // 阻止默认的输入显示
        // 插入
        const text = e.data;
        if (inputType === 'insertText') {
            if (text) {
                // 编辑层提供的api，执行插入操作
                this.editor.insertTextAtCursor(text);
            }
            // 删除
        } else if (inputType === 'deleteContentBackward') {
           this.editor.deleteTextAtCursor()
            // 按下回车, 换行   
        } else if (inputType === 'insertParagraph') {
            this.editor.insertTextAtCursor('\n');
        }
    }
    addListeners() {
        this.container.addEventListener('beforeinput', this.beforeInputHandler_.bind(this))
    }
}
export default KeyboardEvent;
